package com.exmaple.baidumv.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.baidumv.R;
import com.exmaple.baidumv.entity.HomeBannerEntity;
import com.yuelan.codelib.utils.ImageDownloader;
import com.yuelan.codelib.utils.ImageDownloader.OnImageDownload;
import com.yuelan.codelib.utils.LOG;

public class HomeAdapter extends BaseAdapter {
	
	private Context mContext ;
	private LayoutInflater mInflater;
	private ArrayList<HomeBannerEntity> arrayList;
	public HomeAdapter(Context context){
		mContext = context;
		mInflater = LayoutInflater.from(mContext);
	}
	
	public void setList(ArrayList<HomeBannerEntity> array ){
		arrayList = array ;
		notifyDataSetChanged();
//		LOG.v(arrayList.toString());
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(arrayList != null){
			return arrayList.size();
		}else {
			return 0;
		}
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		final ViewHolder viewHolder;
		if(arg1 == null){
			viewHolder = new ViewHolder();
			arg1 = mInflater.inflate(R.layout.item_home_grid , null,false);
			viewHolder.mMoviePicture = (ImageView)arg1.findViewById(R.id.movie_picture);
			viewHolder.mMovieTime = (TextView)arg1.findViewById(R.id.movie_time);
			viewHolder.mMovieName = (TextView)arg1.findViewById(R.id.movie_name);
			viewHolder.mClickPlay = (TextView)arg1.findViewById(R.id.click_play);
			arg1.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder)arg1.getTag();
		}
		ImageDownloader.imageDownload(arrayList.get(arg0).getImgUrl(),mContext,new OnImageDownload() {

			@Override
			public void onDownloadSucc(Bitmap bitmap) {
				if (bitmap != null) {
					viewHolder.mMoviePicture.setImageBitmap(bitmap);
				}
			}
		}, 800);
		viewHolder.mMovieTime.setText(arrayList.get(arg0).getTimes());
		viewHolder.mMovieName.setText(arrayList.get(arg0).getName());
		
		
		return arg1;
	}
	class ViewHolder{
		private ImageView mMoviePicture;
		private TextView mMovieTime;
		private TextView mMovieName;
		private TextView mClickPlay ;
		
	}

}
